﻿using System;

namespace SS4U.Controls
{
    public partial class BrowserSearch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var searchPhrase = Request.QueryString["q"];
            umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Debug, 0, "Performing Browser Search. SearchText:" + searchPhrase);

            var searcher = new Library.Searcher();
            var results = searcher.Search(searchPhrase.ToLower(), null, null);

            Response.ContentType = "application/xml; charset=utf-8";

            umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Debug, 0, "Browser Search. Gefunden:" + results.Count);

            Response.Write("<?xml version=\"1.0\"?><SearchSuggestion xmlns=\"http://schemas.microsoft.com/Search/2008/suggestions\">");
            Response.Write("<Query>" + searchPhrase + "</Query>");
            Response.Write("<Section>");
            foreach (var item in results)
            {
                Response.Write("<Item>");
                Response.Write("<Text>" + item.Text + "</Text>");
                Response.Write("<Description>" + item.Category + "</Description>");
                Response.Write("<Url>" + item.LinkUrl + "</Url>");
                Response.Write("</Item>");
            }

            Response.Write("</Section>");
            Response.Write("</SearchSuggestion>");

        }
    }
}
